/* The file is part of the Waff web framework.
 *
 * Copyright (c) 2011, Grzegorz Skorupa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditionsare met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of conditions
 *   and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *   and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Grzegorz Skorupa nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pl.org.waff;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author greg
 */
public class PathElement {

    private ArrayList elements; // kolejne elementy ścieśki
    private ArrayList elementPaths; // kolejne podścieżki
    private String policy;
    private int level = 0;
    private String parentPath = null;
    private String name;
    private String path;
    private String template;
    private String mobileTemplate;
    private boolean visible;

    public PathElement(String path, String name, String policy, 
            String templateLocation, String mobileTemplateLocation, 
            Properties properties) {
        boolean firstElement = true;
        this.policy = policy;
        this.path = path;
        this.name = name;
        if (name.startsWith("#")) {
            visible = false;
            this.name=name.substring(1);
        } else {
            visible = true;
        }
        this.template = templateLocation;
        this.mobileTemplate = mobileTemplateLocation;
        elements = new ArrayList();
        elementPaths = new ArrayList();
        StringBuffer subpath = new StringBuffer();
        StringTokenizer st = new StringTokenizer(path, "/");
        String sElement;
        String elementName;
        int separatorPos = -1;
        subpath.append("/");
        //
        elementName = properties.getProperty(subpath.toString());
        separatorPos = elementName.indexOf(":");
        if (separatorPos > 0) {
            elementName = elementName.substring(0, separatorPos);

        } else {
            elementName = subpath.toString();
        }
        elements.add(elementName);
        elementPaths.add(subpath.toString());
        //
        try {
            while (st.hasMoreTokens()) {
                sElement = st.nextToken();
                if (!firstElement) {
                    subpath.append("/");
                    level++;
                }
                subpath.append(sElement);
                //
                elementName = properties.getProperty(subpath.toString());
                separatorPos = elementName.indexOf(":");
                if (separatorPos > 0) {
                    elementName = elementName.substring(0, separatorPos);

                } else {
                    elementName = subpath.toString();
                }
                elementPaths.add(subpath.toString());
                elements.add(elementName);
                //
                firstElement = false;
            }
            if (elementPaths.size() > 1) {
                parentPath = (String) elementPaths.get(elementPaths.size() - 2);
            }
        } catch (Exception e) {
            //TODO: error handling   
        }
    }

    /**
     * @return the policy
     */
    public String getPolicy() {
        return policy;
    }

    /**
     * @return the level
     */
    public int getLevel() {
        return level;
    }

    /**
     * @return the parentPath
     */
    public String getParentPath() {
        return parentPath;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }

    /**
     * @return the elements
     */
    public ArrayList getElements() {
        return elements;
    }

    /**
     * @return the elementPaths
     */
    public ArrayList getElementPaths() {
        return elementPaths;
    }

    /**
     * @param parentPath the parentPath to set
     */
    public void setParentPath(String parentPath) {
        this.parentPath = parentPath;
    }

    /**
     * @return the template
     */
    public String getTemplate() {
        return template;
    }

    /**
     * @param template the template to set
     */
    public void setTemplate(String template) {
        this.template = template;
    }

    /**
     * @return the visible
     */
    public boolean isVisible() {
        return visible;
    }

    /**
     * @param visible the visible to set
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    /**
     * @return the mobileTemplate
     */
    public String getMobileTemplate() {
        return mobileTemplate;
    }

    /**
     * @param mobileTemplate the mobileTemplate to set
     */
    public void setMobileTemplate(String mobileTemplate) {
        this.mobileTemplate = mobileTemplate;
    }
}
